import Vue from 'vue';
import Vuex from 'vuex';
import lodash from 'lodash';
import { getStudyProgress } from '@/api/openApi';
import { queryExamCourseList } from '@/api/exam/testExamCourse';
import { queryDurationList, addedDurationLog } from '@/api/front/sysDurationLog';
Vue.use(Vuex)
const state = {}
const actions = {
	//新增学时记录
	async addedDurationRecord({ rootGetters, commit }, { resId, durationType, duration }) {
		let userId = rootGetters.userInfo?.id;
		let object = { userId, resId, durationType };
		let params = { page: 1, limit: 1, ...object };
		let result = await queryDurationList(params).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			if (lodash.isEmpty(datas)) {
				addedDurationLog({ ...object, duration });
			}
		}
	},

	//查询课程考试
	async getCourseExamData({ commit }, data) {
		let params = { page: 1, limit: 1, ...data };
		let result = await queryExamCourseList(params).then(res => res.data);
		if (result.code === '0') {
			return Promise.resolve(Vue.prototype.$detailData(result.data));
		}
	},

	//获取未学习章节
	async getUnlearnChapter({ rootGetters, commit }, { courseId, nodes }) {
		let userName = rootGetters.userInfo?.id;
		let params = { page: 1, limit: 999, userName, courseId };
		let result = await getStudyProgress(params).then(res => res.data);
		console.log(result);
		if (result.code === '0') {
			let unfinish = {};
			let datas = result.data || [];
			let finishs = datas.filter(n => n.status === '1');
			for (let i = 0; i < nodes.length; i++) {
				if (!finishs.some((n) => n.chapterId === nodes[i].id)) {
					unfinish = nodes[i] || {};
					break;
				}
			}
			return Promise.resolve(unfinish);
		}
	}
}
const mutations = {}
export default { state, actions, mutations }